package com.ruoyi.web.controller.system;

import java.util.*;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.TempUserSalaryDto;
import com.ruoyi.system.service.IAchievementsService;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Payroll;
import com.ruoyi.system.service.IPayrollService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 工资Controller
 * 
 * @author ruoyi
 * @date 2022-11-13
 */
@Controller
@RequestMapping("/system/payroll")
public class PayrollController extends BaseController
{
    private String prefix = "system/payroll";

    @Autowired
    private IPayrollService payrollService;
    @Autowired
    private IAchievementsService achievementsService;

    @RequiresPermissions("system:payroll:view")
    @GetMapping()
    public String payroll()
    {
        return prefix + "/payroll";
    }

    /**
     * 查询工资列表
     */
    @RequiresPermissions("system:payroll:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Payroll payroll)
    {
        startPage();
        List<Payroll> list = payrollService.selectPayrollList(payroll);
        return getDataTable(list);
    }



    @PostMapping("/createSalaryMap")
    @ResponseBody
    @Transactional(rollbackFor = Exception.class)
    @ApiOperation("创建工资发放表")
    public AjaxResult createSalaryMap() {
        Map<String,Object> map=new HashMap<>();
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH)+1;
        String dateStr=year+"-"+month+"-01";
        Date date = DateUtils.parseDate(dateStr);
        map.put("YYYYMM", date);
        List<TempUserSalaryDto> datas = achievementsService.selectAchievementsData(map);
        return AjaxResult.success(payrollService.createSalaryMap(datas));
    }



    /**
     * 导出工资列表
     */
    @RequiresPermissions("system:payroll:export")
    @Log(title = "工资", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Payroll payroll)
    {
        List<Payroll> list = payrollService.selectPayrollList(payroll);
        ExcelUtil<Payroll> util = new ExcelUtil<Payroll>(Payroll.class);
        return util.exportExcel(list, "工资数据");
    }

    /**
     * 新增工资
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存工资
     */
    @RequiresPermissions("system:payroll:add")
    @Log(title = "工资", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Payroll payroll)
    {
        return toAjax(payrollService.insertPayroll(payroll));
    }

    /**
     * 修改工资
     */
    @RequiresPermissions("system:payroll:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        Payroll payroll = payrollService.selectPayrollById(id);
        mmap.put("payroll", payroll);
        return prefix + "/edit";
    }

    /**
     * 修改保存工资
     */
    @RequiresPermissions("system:payroll:edit")
    @Log(title = "工资", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Payroll payroll)
    {
        return toAjax(payrollService.updatePayroll(payroll));
    }

    /**
     * 删除工资
     */
    @RequiresPermissions("system:payroll:remove")
    @Log(title = "工资", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(payrollService.deletePayrollByIds(ids));
    }
}
